package com.jeecms.wicool.manager.product.impl;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.product.ProductCateDao;
import com.jeecms.wicool.entity.product.ProductCate;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.product.ProductCateMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

@Service
@Transactional
public class ProductCateMngImpl implements ProductCateMng {
	
	private ProductCateDao dao;
	@Autowired
	public void setDao(ProductCateDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(String cateName,Integer cateLevel,int pageNo, int pageSize,CmsUser user) {
		Pagination page = dao.getPage(cateName,cateLevel,pageNo, pageSize,user);
		return page;
	}
	@Override
	public ProductCate findById(Integer id) {
		ProductCate entity = dao.findById(id);
		return entity;
	}

	@Override
	public ProductCate update(ProductCate bean,Integer shopId) {
		//根据bean的父级Id 获取父级类别的等级
		ProductCate beanParent =findById(bean.getParentId());
		if(beanParent!=null){
			bean.setCateLevel(beanParent.getCateLevel()+1);
		}else{
			bean.setCateLevel(1);
		}
		
		ShopInfo shop = shopMng.findById(shopId);
		bean.setShop(shop);
		Updater<ProductCate> updater = new Updater<ProductCate>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

	@Override
	public ProductCate save(ProductCate bean,Integer shopId) {
		//根据bean的父级Id 获取父级类别的等级
		ProductCate beanParent =findById(bean.getParentId());
		if(beanParent!=null){
			bean.setCateLevel(beanParent.getCateLevel()+1);
		}else{
		bean.setCateLevel(1);
		}
		ShopInfo shop = shopMng.findById(shopId);
		bean.setShop(shop);
		dao.save(bean);
		return bean;
	}

	@Override
	public List<ProductCate> getListByCateLevel(Integer level,CmsUser user) {
		return dao.getListByCateLevel(level,user);
	}
	@Override
	public ProductCate deleteById(Integer id) {
		ProductCate bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public ProductCate[] deleteByIds(Integer[] ids) {
		ProductCate[] beans = new ProductCate[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

    @Override
    public List<ProductCate> getAll() {
        return dao.getAll();
    }

    @Autowired
	private WiShopInfoMng shopMng;
}